package com.yingli.network;

import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity1 extends AppCompatActivity {
    //图片资源
    private int[] image = {R.drawable.bz,R.drawable.bz1,R.drawable.bz2,
                R.drawable.bz3,R.drawable.bz4,R.drawable.bz4,R.drawable.bz5 ,
                R.drawable.bz6,R.drawable.bz7};
    //存放图片
    private int current = 0;
    //定义 好 初始化两个按钮
    private Button bt1;
    private Button bt2;

    private LinearLayout mLinearLayout1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lianxi);
        //标题
        setTitle("使用XML布局文件和java代码共同控制UI界面");

        //初始化LinearLayout和按钮
        mLinearLayout1 = findViewById(R.id.layout1);
        bt1 = findViewById(R.id.bt1);
        bt2 = findViewById(R.id.bt2);
        final ImageView imageView = new ImageView(this);

        //布局加载ImageView组件
        mLinearLayout1.addView(imageView);
        //ImageView 加载第一张图片
        imageView.setImageResource(image[current]);
        //ImageView 的点击事件
        imageView.setOnClickListener(v -> {
            if (current >= image.length -1){
                current = -1;
            }
            imageView.setImageResource(image[++current]);
        });
        //上一张按钮 的点击事件
        bt1.setOnClickListener(v -> {
            if (current <= 0){
                current = 1;
                Toast.makeText(this, "图片浏览完毕", Toast.LENGTH_SHORT).show();
            }
            imageView.setImageResource(image[--current]);
        });
        //下一张按钮 的点击事件
        bt2.setOnClickListener(v -> {
            if (current >= image.length - 1){
                current = image.length -2;
                Toast.makeText(this, "图片浏览完毕", Toast.LENGTH_SHORT).show();
            }
            imageView.setImageResource(image[++current]);
        });
        textView = findViewById(R.id.a1);
        textView.setOnClickListener(v -> {
            Toast.makeText(this, "抱歉！没有内容可展示", Toast.LENGTH_SHORT).show();
        });
    }
    private TextView textView;
}
